from django.db import models
import django.contrib.auth
# Create your models here.

class User(models.Model):
    auth_user = models.OneToOneField(django.contrib.auth.models.User)
    student_id = models.CharField(max_length=30)
    password = models.CharField(max_length=30)
    school = models.CharField(max_length=30)
    major = models.CharField(max_length=30)
    name = models.CharField(max_length=30)
    email = models.EmailField()
    group = models.ForeignKey('Group',null=True,related_name = 'users')
    last_updated_time = models.DateTimeField(auto_now_add = True)

    def __unicode__(self):
        return ' '.join([self.student_id,self.name,self.email])  

class Group(models.Model):
    administrator = models.ForeignKey(User,related_name = 'group_admined')
    name = models.CharField(max_length=30)
    introduction = models.TextField()
    last_updated_time = models.DateTimeField(auto_now_add = True)
    
    def __unicode__(self):
        return self.name

class Homework(models.Model):
    name = models.CharField(max_length=30)
    content = models.TextField()
    pub_date = models.DateTimeField(auto_now_add = True)
    deadline = models.DateTimeField()
    last_updated_time = models.DateTimeField(auto_now_add = True)
    
    #personal or teamwork
    type = models.CharField(max_length=30)
    def __unicode__(self):
        return self.name

class HomeworkSubmitedInfo(models.Model):
    last_updated_time = models.DateTimeField(auto_now_add = True)
    homework = models.ForeignKey(Homework)
    src = models.CharField(max_length=1024)
    url = models.CharField(max_length=1024)
    pic = models.CharField(max_length=1024)
    #be calculated after deadline
    score = models.IntegerField()
    rank = models.IntegerField()
    ranking_in_group = models.IntegerField(null = True)

class PersonalHomeworkSubmited(models.Model):
    homework_submited_info = models.ForeignKey(HomeworkSubmitedInfo)
    user = models.ForeignKey(User)

class Contribution(models.Model):
    user = models.ForeignKey(User)
    contribution = models.IntegerField()

class TeamworkSubmited(models.Model):
    homework_submited_info = models.ForeignKey(HomeworkSubmitedInfo)
    group = models.ForeignKey(Group)
    contributions = models.ManyToManyField(Contribution)

class Assessment(models.Model):
    homework = models.ForeignKey(Homework)
    group = models.ForeignKey(Group)
    highest_score = models.IntegerField()
    lowest_score = models.IntegerField()
    ranking_check = models.BooleanField()
    remarks = models.CharField(max_length=1024,null=True)
    assessor = models.ForeignKey(User,null=True)
    last_updated_time = models.DateTimeField(auto_now_add = True)

class Settings(models.Model):
    allow_register = models.BooleanField()
    allow_create_group = models.BooleanField()
    allow_join_group = models.BooleanField() 
    allow_quit_group = models.BooleanField() 
    allow_delete_group = models.BooleanField() 
    allow_edit_group = models.BooleanField() 
    number_of_group_member = models.IntegerField()
    user_number_per_page = models.IntegerField()
    group_number_per_page = models.IntegerField()
    homework_number_per_page = models.IntegerField()
    root_path_of_homework = models.CharField(max_length=1024)
    root_path_of_src_file = models.CharField(max_length=1024)
    url_of_homework = models.CharField(max_length=1024)
    url_of_src_file = models.CharField(max_length=1024)
    
class Comment(models.Model):
    
    author = models.ForeignKey(User,related_name = 'comment')
    homework = models.ForeignKey(Homework, related_name = 'comment' )
    parent_comment = models.ForeignKey('self',null=True)
    content = models.TextField()
    pub_date = models.DateTimeField(auto_now_add = True)
    depth = models.IntegerField()
    

    